package org.soenea.doitall.domain.command;

import java.util.List;

import org.dsrg.soenea.domain.MapperException;
import org.dsrg.soenea.domain.command.CommandError;
import org.dsrg.soenea.domain.command.CommandException;
import org.dsrg.soenea.domain.command.DomainCommand;
import org.dsrg.soenea.domain.helper.Helper;
import org.dsrg.soenea.service.SingleAppUniqueIdFactory;
import org.soenea.doitall.domain.model.group.Group;
import org.soenea.doitall.domain.model.group.GroupProxy;
import org.soenea.doitall.domain.model.group.IGroup;
import org.soenea.doitall.domain.model.group.data.GroupTDG;
import org.soenea.doitall.domain.model.group.mapper.GroupInputMapper;
import org.soenea.doitall.domain.model.group.mapper.GroupOutputMapper;
import org.soenea.doitall.domain.model.invite.Invite;
import org.soenea.doitall.domain.model.invite.InviteListProxy;
import org.soenea.doitall.domain.model.person.Person;
import org.soenea.doitall.domain.model.person.mapper.PersonInputMapper;

public class AddGroupCommand extends DomainCommand {

	public AddGroupCommand(Helper helper) {
		super(helper);
	}

	@Override
	public void setUp() throws CommandException {
	}

	@Override
	public void process() throws CommandException {
		try {
			String name = helper.getString("name");
			String description = helper.getString("description");
			long id = 0L;
			try{
				id = SingleAppUniqueIdFactory.getMaxId(GroupTDG.BASE_NAME, GroupTDG.GROUP_ID);
			}catch( Exception e){
				//supress exception
			}
			IGroup thisGroup = new GroupProxy(id);
			Group group = new Group(id, 0L, name, description, new InviteListProxy(thisGroup));
			GroupOutputMapper.insert(group);
			helper.setRequestAttribute("groups", GroupInputMapper.findAll());
		} catch (MapperException e) {
			throw new CommandException(e);
		}
	}

	@Override
	public void tearDown() throws CommandError {
	}

}
